package com.shensi.adminCollect.service.coupon.impl;

import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.domain.coupon.CouponCreation;
import com.shensi.adminCollect.domain.coupon.CouponExport;
import com.shensi.adminCollect.domain.coupon.CouponInfo;
import com.shensi.adminCollect.domain.coupon.CouponIssue;
import com.shensi.adminCollect.domain.coupon.vo.CouponIssueInfoVO;
import com.shensi.adminCollect.mapper.coupon.CouponIssueMapper;
import com.shensi.adminCollect.service.coupon.ICouponIssueService;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Random;

/**
 * 优惠劵发放方式Service业务层处理
 * 
 * @author qujia
 * @date 2021-08-13
 */


@Service
public class CouponIssueServiceImpl implements ICouponIssueService 
{
    @Autowired
    private CouponIssueMapper couponIssueMapper;

    /**
     * 查询优惠劵发放方式
     * 
     * @param id 优惠劵发放方式ID
     * @return 优惠劵发放方式
     */
    @Override
    public CouponIssue selectCouponIssueById(String id)
    {
        return couponIssueMapper.selectCouponIssueById(id);
    }

    @Override
    public CouponIssue selectIssueById(Long id)
    {
        return couponIssueMapper.selectIssueById(id);
    }
    /**
     * 查询优惠劵发放方式
     *
     * @param issueId 优惠劵发放方式ID
     *   @param status 状态
     * @return 优惠劵发放方式
     */
    @Override
    public void updateCouponStatus(Long issueId, String  status) {
        couponIssueMapper.updateCouponIssueStatus(issueId, status);
//        couponIssueMapper.updateCouponInfoStatus(issueId, status);
    }

    @Override
    public void updateCouponActivityStatus(Long issueId, String  status) {
        couponIssueMapper.updateCouponActivityStatus(issueId, status);
//        couponIssueMapper.updateCouponInfoStatus(issueId, status);
    }
    /**
     * 查询优惠劵发放方式
     *
     * @param couponInfo
     * @return CouponCreation
     */
    @Override
    public CouponIssue createNewCouponIssue(CouponCreation couponInfo)
    {
        CouponIssue couponIssue = new CouponIssue();
//        CouponIssue couponIssue.ins
        int i=0;
        Random random = new Random();
        String result = "";
        String result1 = "";
        for (int m = 0; m < 4; m++) {
            result += random.nextInt(10);
        }
        for (int mm = 0; mm < 4; mm++) {
            result1 += random.nextInt(10);
        }
        String TempTime=System.currentTimeMillis()+"";
        String ordNo = ""+result1+TempTime.substring(5)+result;
        couponIssue.setId(ordNo);
        LoginUser currentUser = SecurityUtils.getLoginUser();
        couponIssue.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        couponIssue.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
//构建数据，来保证我没用优惠券活动的时候可以发放优惠券
        couponIssue.setIssueName(couponInfo.getCouponRemark());
        couponIssue.setIssueType("2");
        couponIssue.setGetThresholdAmt(new BigDecimal("0.00"));
        couponIssue.setCouponThresholdAmt(couponInfo.getCouponThresholdAmt());
        couponIssue.setStatus("0");
        couponIssue.setShowId(null);
        couponIssue.setGiveCount(couponInfo.getGiveCount());
        couponIssue.setGiveLimit(couponInfo.getCouponNum());
        couponIssue.setIssueRemark(couponInfo.getCouponRemark());
        couponIssue.setAntiCheating("1");
//        effectiveTimeStart
            couponIssue.setStartDate(couponInfo.getStartDate());
            couponIssue.setEndDate(couponInfo.getEndDate());
//        if (couponInfo.getCountOfDay() != null) {
//            couponIssue.setCountOfDay(couponInfo.getCountOfDay());
//        } else {
////            默认防作弊
//            couponIssue.setGiveCount(1L);
//        }
        couponIssue.setCountOfDay(999L);
        couponIssue.setMemberCondition("all");;
        couponIssue.setThresholdType("");
        couponIssue.setAntiMonthCheating("0");
        couponIssue.setCountOfMonth(999);

        couponIssue.setOilPartOrAll(0L);
        return couponIssue;
    }
    /**
     * 查询优惠劵发放方式
     *
     * @param couponInfo
     * @return CouponCreation
     */
    @Override
    public CouponIssue updateNewCouponIssue(CouponCreation couponInfo)
    {
        CouponIssue couponIssue = new CouponIssue();
//
        couponIssue.setId(couponInfo.getId());
        LoginUser currentUser = SecurityUtils.getLoginUser();
        couponIssue.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        couponIssue.setUpdateBy(String.valueOf(currentUser.getUser().getUserId()));
//构建数据，来保证我没用优惠券活动的时候可以发放优惠券
        couponIssue.setIssueName(couponInfo.getCouponRemark());
        couponIssue.setIssueType("2");
        couponIssue.setGetThresholdAmt(new BigDecimal("0.00"));
        couponIssue.setCouponThresholdAmt(couponInfo.getCouponThresholdAmt());
//        couponIssue.setStatus(couponInfo.getStatus());
        couponIssue.setShowId(null);
        couponIssue.setGiveCount(couponInfo.getGiveCount());
        couponIssue.setGiveLimit(couponInfo.getCouponNum());
        couponIssue.setIssueRemark(couponInfo.getCouponRemark());
        couponIssue.setAntiCheating("1");
//        effectiveTimeStart
        couponIssue.setStartDate(couponInfo.getStartDate());
        couponIssue.setEndDate(couponInfo.getEndDate());

        couponIssue.setMemberCondition("all");;
        couponIssue.setThresholdType("");
        couponIssue.setCountOfDay(999L);
        couponIssue.setAntiMonthCheating("0");
        couponIssue.setCountOfMonth(999);

        couponIssue.setOilPartOrAll(0L);
        return couponIssue;
    }


    /**
     * 查询优惠劵发放方式列表
     * 
     * @param couponIssue 优惠劵发放方式
     * @return 优惠劵发放方式
     */
    @Override
    public List<CouponIssue> selectCouponIssueList(CouponIssue couponIssue)
    {
        return couponIssueMapper.selectCouponIssueList(couponIssue);
    }

    /**
     * 查询优惠劵发放方式列表
     *
     * @param couponcreation 优惠劵发放方式
     * @return 优惠劵发放方式
     */
    @Override
    public List<CouponCreation> selectCouponIssueListByCreation(CouponCreation couponcreation)
    {
        return couponIssueMapper.selectCouponIssueListByCreation(couponcreation);
    }
    /**
     * 新增优惠劵发放方式
     *
     * @param ids 优惠劵发放ids
     * @return 结果
     */
    @Override
    public List<CouponExport> getCouponIssueListByIds(List<String> ids) {
        List<CouponCreation> couponCreations = couponIssueMapper.selectCouponIssueListByIds(ids);
        List<CouponExport> couponExports = new ArrayList<>();

        // 实例化 Base64
        Base64.Encoder encoder = Base64.getEncoder();

        for (CouponCreation couponCreation : couponCreations) {
            CouponExport couponExport = new CouponExport();
            String id = couponCreation.getId();
            String encodedId = encryptId(Long.parseLong(id), encoder);
            couponExport.setId(id);
            couponExport.setEncodedId(encodedId);
            couponExport.setCouponRemark(couponCreation.getCouponRemark());
            couponExport.setStationName(couponCreation.getStationName());
            couponExports.add(couponExport);
        }

        return couponExports;
    }
    // 加密方法
    private String encryptId(Long id, Base64.Encoder encoder) {
        String encodedStr = encoder.encodeToString(("i" + id).getBytes());
        String trimEqualStr = encodedStr.replaceAll("={1,}$", "");
        return trimEqualStr;
    }
    /**
     * 新增优惠劵发放方式
     * 
     * @param couponIssue 优惠劵发放方式
     * @return 结果
     */
    @Override
    public int insertCouponIssue(CouponIssue couponIssue)
    {
        couponIssue.setCreateTime(DateUtils.getNowDate());
        return couponIssueMapper.insertCouponIssue(couponIssue);
    }

    /**
     * 修改优惠劵发放方式
     * 
     * @param couponIssue 优惠劵发放方式
     * @return 结果
     */
    @Override
    public int updateCouponIssue(CouponIssue couponIssue)
    {
        couponIssue.setUpdateTime(DateUtils.getNowDate());
        return couponIssueMapper.updateCouponIssue(couponIssue);
    }
    /**
     * 修改优惠劵发放方式
     *
     * @param issueId 查另一个id
     * @return 结果
     */
    @Override
    public Integer getCouponInfoIdByIssueId(Long issueId) {
        return couponIssueMapper.getCouponInfoIdByIssueId(issueId);
    }

    @Override
    public Integer getCouponActivityByIssueId(Long issueId) {
        return couponIssueMapper.getCouponActivityByIssueId(issueId);
    }
    /**
     * 删除优惠劵关联关系
     *
     * @param issueId 查另一个id
     * @return 结果
     */
    @Override
    public Integer  deleteIssueOilRelationsByIssueId(Long issueId) {
        couponIssueMapper.getCouponActivityByIssueId(issueId);
        return couponIssueMapper.deleteIssueOilRelationsByIssueId(issueId);
    }
    /**
     * 批量删除优惠劵发放方式
     *
     * @param ids 需要删除的优惠劵发放方式ID
     * @return 结果
     */
    @Override
    public int deleteCouponIssueByIds(Integer[] ids)
    {
        return couponIssueMapper.deleteCouponIssueByIds(ids);
    }
    /**
     * 批量删除优惠劵发放方式
     *
     * @param ids 需要删除的优惠劵发放方式ID
     * @return 结果
     */
    @Override
    public int deleteCouponIssueByIssueIds(Long[] ids)
    {
        return couponIssueMapper.deleteCouponIssueByIssueIds(ids);
    }
    /**
     * 删除优惠劵发放方式信息
     * 
     * @param id 优惠劵发放方式ID
     * @return 结果
     */
    @Override
    public int deleteCouponIssueById(Integer id)
    {
        return couponIssueMapper.deleteCouponIssueById(id);
    }

    /**
     * 查询发布过的优惠劵发放方式列表
     *
     * @param couponIssue 优惠劵发放方式
     * @return 优惠劵发放方式
     */
    @Override
    public List<CouponIssue> selectCouponIssueList4Relation(CouponIssue couponIssue)
    {
        return couponIssueMapper.selectCouponIssueList4Relation(couponIssue);
    }
    @Override
    public CouponCreation fullCouponCreationForCouponIssue(CouponIssue couponIssue) {
        CouponCreation couponCreation = new CouponCreation();
        couponCreation.setId(couponIssue.getId());
        couponCreation.setAppointedDaysList(couponIssue.getAppointedDaysList());
        couponCreation.setCycleDaysList(couponIssue.getCycleDaysList());
        couponCreation.setOilIdLists(couponIssue.getOilIdLists());
        couponCreation.setCountOfDay(couponIssue.getCountOfDay());
        couponCreation.setStartDate(couponIssue.getStartDate());
        couponCreation.setEndDate(couponIssue.getEndDate());
        couponCreation.setGiveCount(couponIssue.getGiveCount());
        couponCreation.setCouponNotOils(couponIssue.getCouponNotOils());
        couponCreation.setCouponThresholdAmt(couponIssue.getCouponThresholdAmt());
        couponCreation.setCountOfMonth(couponIssue.getCountOfMonth());
        couponCreation.setOilPartOrAll(couponIssue.getOilPartOrAll());
        couponCreation.setShowId(couponIssue.getShowId());
        couponCreation.setMemberLevel(couponIssue.getMemberLevel());
        couponCreation.setPaymentMethod(couponIssue.getPaymentMethod());
        couponCreation.setMemberTags(couponIssue.getMemberTags());
        couponCreation.setShowIdList(couponIssue.getShowIdList());
        couponCreation.setMemberLevelList(couponIssue.getMemberLevelList());
        couponCreation.setPaymentMethodList(couponIssue.getPaymentMethodList());
        couponCreation.setMemberTagsList(couponIssue.getMemberTagsList());
        return couponCreation;
    }

    @Override
    public CouponCreation fullCouponCreationForCouponInfo(CouponCreation couponCreation, CouponInfo couponInfo) {
        couponCreation.setCouponId(couponInfo.getId());
        couponCreation.setCouponName(couponInfo.getCouponName());
        couponCreation.setCouponType(couponInfo.getCouponType());
        couponCreation.setCouponAmt(couponInfo.getCouponAmt());
        couponCreation.setCouponDetails(couponInfo.getCouponDetails());
        couponCreation.setUsageDescription(couponInfo.getUsageDescription());

        couponCreation.setCouponNum(couponInfo.getCouponNum());
        couponCreation.setEffectiveTimeType(couponInfo.getEffectiveTimeType());
        couponCreation.setEffectiveTimeStart(couponInfo.getEffectiveTimeStart());
        couponCreation.setEffectiveTimeEnd(couponInfo.getEffectiveTimeEnd());
        couponCreation.setEffectiveDayNum(couponInfo.getEffectiveDayNum());
        couponCreation.setGetCouponNum(couponInfo.getGetCouponNum());
        couponCreation.setUseCouponNum(couponInfo.getUseCouponNum());
        couponCreation.setCouponDetails(couponInfo.getCouponDetails());
        couponCreation.setStatus(couponInfo.getStatus());
        couponCreation.setCouponRemark(couponInfo.getCouponRemark());
        couponCreation.setDelFlag(couponInfo.getDelFlag());
        couponCreation.setEnterpriseId(couponInfo.getEnterpriseId());
        couponCreation.setVerification(couponInfo.getVerification());
        couponCreation.setVerificationList(couponInfo.getVerificationList());
        couponCreation.setVerificationNameList(couponInfo.getVerificationNameList());
        couponCreation.setMinUseCouponAmt(couponInfo.getMinUseCouponAmt());
        couponCreation.setEffectiveTime(couponInfo.getEffectiveTime());
        couponCreation.setEditFlag(couponInfo.getEditFlag());
        couponCreation.setAppointedDays(couponInfo.getAppointedDays());
        couponCreation.setCycleDays(couponInfo.getCycleDays());
        couponCreation.setAvailableControl(couponInfo.getAvailableControl());
        return couponCreation;
    }

    @Override
    public CouponIssueInfoVO selectCouponIssueByIdWithInfo(String issueId) {
        return couponIssueMapper.selectCouponIssueByIdWithInfo(issueId);
    }


}
